//
//  PictureSelectView.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/3/5.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

class PictureSelectView: UIView, CustomView {
    let lineViewHeight: CGFloat = 1
    private let cellHeight: CGFloat = 40
    private let titles = ["拍照", "从相册选取照片", "取消"]
    internal var mainView: UIView = UIView()
    internal var isBottomAnimation = true
    var tapIndexCloSure: ((NSInteger) -> Void)?
    func showPictureView(tapIndexCloSure: @escaping (NSInteger) -> Void) {
        self.tapIndexCloSure = tapIndexCloSure
        let mainViewHeight = cellHeight * CGFloat(titles.count)
        setBasicDataWith(mainViewRect: CGRect(x: 0, y: ScreenHeight - mainViewHeight, width: ScreenWidth, height: mainViewHeight))
        
        for (index, title) in titles.enumerated() {
            let view = singleView(title: title)
            view.origin = CGPoint(x: 0, y: CGFloat(index) * cellHeight)
            view.tag = index
            mainView.addSubview(view)
        }
    }
    
    /// 设置每一行的视图
    ///
    /// - Parameter title: 文字内容
    /// - Returns: 返回的视图
    private func singleView(title: String) -> UIView {
        let contentView = UIView()
        contentView.size = CGSize(width:  mainView.width, height: cellHeight)
        contentView.backgroundColor = UIColor.white
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapView(_:)))
        contentView.addGestureRecognizer(tap)
        
        let label = UILabel()
        label.text = title
        label.font = UIFont.systemFont(ofSize: 15)
        label.textColor = UIColor.gray
        label.sizeToFit()
        label.center = CGPoint(x: contentView.width / 2, y: contentView.height / 2)
        contentView.addSubview(label)
        
        let lineView = UIView()
        lineView.backgroundColor = ONColor.cutLineColor
        lineView.frame = CGRect(x: 0, y: contentView.height - lineViewHeight, width: contentView.width, height: lineViewHeight)
        contentView.addSubview(lineView)
        return contentView
    }
    
    @objc func tapView(_ gr: UIGestureRecognizer) {
        self.tapIndexCloSure?(gr.view!.tag)
        hide()

    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let view = touches.first?.view {
            if view == self {
                hide()
            }
        }
    }

}
